<!--
 * @Descripttion: (工艺路线/cks_technical_flow_nodes)
 * @Author: (admin)
 * @Date: (2025-06-04)
-->
<template>
  <div>

    <!-- 搜索区域 -->
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item :label="$t('process.ckstechnicalflownodesPage.page.notes')" prop="notes">
        <el-input v-model="queryParams.notes" @keyup.enter="handleQuery"
          :placeholder="$t('process.ckstechnicalflownodesPage.page.notesInput')" clearable style="width: 200px" />
      </el-form-item>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>

      <el-form-item v-hasPermi="['ckstechnicalflownodes:allowreprocess']">

        <el-tooltip :content="$t('process.ckstechnicalflownodesPage.page.allowPCBALineReprocessTip')" placement="top">
          <el-form-item style="display: flex;justify-content: center;align-items: center;"
            :label="$t('process.ckstechnicalflownodesPage.page.allowPCBALineReprocess')">
            <el-switch v-model="pcbaValue" @change="handleAllowReprocess($event, 1)" size="large" inline-prompt
              :active-text="$t('common.yes')" :inactive-text="$t('common.no')" />
          </el-form-item>
        </el-tooltip>

        <el-tooltip :content="$t('process.ckstechnicalflownodesPage.page.allowCoolingribLineReprocessTip')"
          placement="top">
          <el-form-item style="display: flex;justify-content: center;align-items: center;"
            :label="$t('process.ckstechnicalflownodesPage.page.allowCoolingribLineReprocess')">
            <el-switch v-model="coolingribValue" @change="handleAllowReprocess($event, 2)" size="large" inline-prompt
              :active-text="$t('common.yes')" :inactive-text="$t('common.no')" />
          </el-form-item>
        </el-tooltip>

        <el-tooltip :content="$t('process.ckstechnicalflownodesPage.page.allowEolLineReprocessTip')" placement="top">
          <el-form-item style="display: flex;justify-content: center;align-items: center;"
            :label="$t('process.ckstechnicalflownodesPage.page.allowEolLineReprocess')">
            <el-switch v-model="eolValue" @change="handleAllowReprocess($event, 3)" size="large" inline-prompt
              :active-text="$t('common.yes')" :inactive-text="$t('common.no')" />
          </el-form-item>
        </el-tooltip>

      </el-form-item>

    </el-form>

    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <!-- <el-button type="primary" v-hasPermi="['ckstechnicalflownodes:add']" plain icon="plus" @click="handleAdd">
            {{ $t('btn.add') }}
          </el-button> -->
      </el-col>
      <!-- <right-toolbar v-if="false" v-model:showSearch="showSearch" @queryTable="getList"
        :columns="columns"></right-toolbar> -->
    </el-row>

    <!-- 表格区域 -->
    <el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
      highlight-current-row @sort-change="sortChange">

      <el-table-column prop="nodeId" :label="$t('process.ckstechnicalflownodesPage.tableColumns.nodeId')" align="center"
        v-if="columns.showColumn('nodeId')" />
      <el-table-column prop="notes" :label="$t('process.ckstechnicalflownodesPage.tableColumns.notes')" align="center"
        :show-overflow-tooltip="true" v-if="columns.showColumn('notes')" />

      <el-table-column prop="allowReprocess" align="center">
        <template #header>
          <span>{{ $t('process.ckstechnicalflownodesPage.tableColumns.allowReprocess') }}</span>
          <el-tooltip :content="$t('process.ckstechnicalflownodesPage.tableColumns.allowReprocessTips')"
            placement="top">
            <el-icon style="cursor:pointer;">
              <Warning />
            </el-icon>
          </el-tooltip>
        </template>

        <template #default="scope">
          <dict-tag :options="options.yesOrNo" :value="scope.row.allowReprocess" />
        </template>
      </el-table-column>

      <el-table-column prop="allowOkReprocess"
        :label="$t('process.ckstechnicalflownodesPage.tableColumns.allowOkReprocess')" align="center">
        <template #default="scope">
          <dict-tag :options="options.yesOrNo" :value="scope.row.allowOkReprocess" />
        </template>
      </el-table-column>

      <el-table-column prop="requireOk" :label="$t('process.ckstechnicalflownodesPage.tableColumns.requireOk')"
        align="center">
        <template #default="scope">
          <dict-tag :options="options.yesOrNo" :value="scope.row.requireOk" />
        </template>
      </el-table-column>

      <el-table-column prop="preNodeId" :label="$t('process.ckstechnicalflownodesPage.tableColumns.preNodeId')"
        align="center"></el-table-column>

      <el-table-column prop="needPrevCheck" :label="$t('process.ckstechnicalflownodesPage.tableColumns.needPrevCheck')"
        align="center">
        <template #default="scope">
          <dict-tag :options="options.yesOrNo" :value="scope.row.needPrevCheck" />
        </template>
      </el-table-column>

      <el-table-column prop="nullity" :label="$t('process.ckstechnicalflownodesPage.tableColumns.nullity')"
        align="center">
        <template #default="scope">
          <dict-tag :options="options.yesOrNo" :value="scope.row.nullity" />
        </template>
      </el-table-column>

      <el-table-column :label="$t('btn.operate')" width="200" align="center">
        <template #default="scope">
          <el-button type="success" size="small" icon="edit" :title="$t('btn.edit')"
            v-hasPermi="['ckstechnicalflownodes:edit']" @click="handleUpdate(scope.row)">{{ $t('btn.edit')
            }}</el-button>
          <!-- <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['ckstechnicalflownodes:delete']" @click="handleDelete(scope.row)"></el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改工艺路线对话框 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="170px">
        <el-row :gutter="20">

          <el-col :lg="12">
            <el-form-item :label="$t('process.ckstechnicalflownodesPage.tableColumns.nodeId')" prop="nodeId">
              <el-input v-model.number="form.nodeId"
                :placeholder="$t('process.ckstechnicalflownodesPage.formDialog.nodeId')" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('process.ckstechnicalflownodesPage.tableColumns.notes')" prop="notes">
              <el-input v-model="form.notes" :placeholder="$t('process.ckstechnicalflownodesPage.formDialog.notes')" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('process.ckstechnicalflownodesPage.tableColumns.allowReprocess')"
              prop="allowReprocess">
              <el-switch v-model="form.allowReprocess" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('process.ckstechnicalflownodesPage.tableColumns.allowOkReprocess')"
              prop="allowOkReprocess">
              <el-switch v-model="form.allowOkReprocess" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('process.ckstechnicalflownodesPage.tableColumns.requireOk')" prop="requireOk">
              <el-switch v-model="form.requireOk" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('process.ckstechnicalflownodesPage.tableColumns.preNodeId')" prop="preNodeId">
              <el-input v-model.number="form.preNodeId"
                :placeholder="$t('process.ckstechnicalflownodesPage.formDialog.preNodeId')" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('process.ckstechnicalflownodesPage.tableColumns.needPrevCheck')"
              prop="needPrevCheck">
              <el-switch v-model="form.needPrevCheck" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label="$t('process.ckstechnicalflownodesPage.tableColumns.nullity')" prop="nullity">
              <el-radio-group v-model="form.nullity">
                <el-radio :value="true">{{ $t('common.disable') }}</el-radio>
                <el-radio :value="false">{{ $t('common.enable') }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="ckstechnicalflownodes">
import {
  listCksTechnicalFlowNodes,
  addCksTechnicalFlowNodes, delCksTechnicalFlowNodes,
  updateCksTechnicalFlowNodes, getCksTechnicalFlowNodes,
  allowReprocess
}
  from '@/api/business/ckstechnicalflownodes.js'
import { alertEffects, ElTooltip } from 'element-plus'
import { t } from 'vxe-table'

const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  notes: '',
  sort: '',
  sortType: 'asc',
})
const columns = ref([
  { visible: true, prop: 'id', label: 'Id' },
  { visible: true, prop: 'nodeId', label: proxy.$t('process.ckstechnicalflownodesPage.tableColumns.nodeId') },
  { visible: true, prop: 'notes', label: proxy.$t('process.ckstechnicalflownodesPage.tableColumns.notes') },
  { visible: true, prop: 'tableId', label: proxy.$t('process.ckstechnicalflownodesPage.tableColumns.tableId') },
  { visible: true, prop: 'allowReprocess', label: proxy.$t('process.ckstechnicalflownodesPage.tableColumns.allowReprocess') },
  { visible: true, prop: 'allowOkReprocess', label: proxy.$t('process.ckstechnicalflownodesPage.tableColumns.allowOkReprocess') },
  { visible: true, prop: 'requireOk', label: proxy.$t('process.ckstechnicalflownodesPage.tableColumns.requireOk') },
  { visible: true, prop: 'preNodeId', label: proxy.$t('process.ckstechnicalflownodesPage.tableColumns.preNodeId') },
  { visible: true, prop: 'needPrevCheck', label: proxy.$t('process.ckstechnicalflownodesPage.tableColumns.needPrevCheck') },
  { visible: true, prop: 'nullity', label: proxy.$t('process.ckstechnicalflownodesPage.tableColumns.nullity') },
  { visible: false, prop: 'createdTime', label: 'CreatedTime' },
  { visible: false, prop: 'isDeleted', label: 'IsDeleted' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])

const pcbaValue = ref(false)
const coolingribValue = ref(false)
const eolValue = ref(false)

var dictParams = []


function getList() {
  loading.value = true
  listCksTechnicalFlowNodes(queryParams).then(res => {
    const { code, data, allowReprocessCount } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum

      pcbaValue.value = (allowReprocessCount.find(o => o.productionLineCode == 'PCBA').count > 1);
      coolingribValue.value = (allowReprocessCount.find(o => o.productionLineCode == 'Radiator').count > 1);
      eolValue.value = (allowReprocessCount.find(o => o.productionLineCode == 'EOL').count > 1);

      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    nodeId: [{ required: true, message: proxy.$t('process.ckstechnicalflownodesPage.formDialog.nodeId'), trigger: "blur", type: "number" }],
    notes: [{ required: true, message: proxy.$t('process.ckstechnicalflownodesPage.formDialog.notes'), trigger: "blur" }],
    tableId: [{ required: true, message: proxy.$t('process.ckstechnicalflownodesPage.formDialog.tableId'), trigger: "blur", type: "number" }],
    allowReprocess: [{ required: true, message: proxy.$t('process.ckstechnicalflownodesPage.formDialog.allowReprocess'), trigger: "blur" }],
    allowOkReprocess: [{ required: true, message: proxy.$t('process.ckstechnicalflownodesPage.formDialog.allowOkReprocess'), trigger: "blur" }],
    requireOk: [{ required: true, message: proxy.$t('process.ckstechnicalflownodesPage.formDialog.requireOk'), trigger: "blur" }],
    preNodeId: [{ required: true, message: proxy.$t('process.ckstechnicalflownodesPage.formDialog.preNodeId'), trigger: "blur", type: "number" }],
    needPrevCheck: [{ required: true, message: proxy.$t('process.ckstechnicalflownodesPage.formDialog.needPrevCheck'), trigger: "blur" }],
    nullity: [{ required: true, message: proxy.$t('process.ckstechnicalflownodesPage.formDialog.nullity'), trigger: "blur" }],
    // createdTime: [{ required: true, message: "CreatedTime不能为空", trigger: "blur" }],
    // isDeleted: [{ required: true, message: "IsDeleted不能为空", trigger: "blur" }],
  },
  options: {
    // IsDeleted 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
    yesOrNo: [
      { dictLabel: proxy.$t('common.yes'), dictValue: 'true' },
      { dictLabel: proxy.$t('common.no'), dictValue: 'false' }
    ],
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    id: null,
    nodeId: null,
    notes: null,
    tableId: null,
    allowReprocess: null,
    allowOkReprocess: null,
    requireOk: null,
    preNodeId: null,
    needPrevCheck: null,
    nullity: null,
    createdTime: null,
    isDeleted: null,
  };
  proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = proxy.$t('process.ckstechnicalflownodesPage.formDialog.addTitle')
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getCksTechnicalFlowNodes(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = proxy.$t('process.ckstechnicalflownodesPage.formDialog.editTitle')
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {

      if (form.value.id != undefined && opertype.value === 2) {
        updateCksTechnicalFlowNodes(form.value).then((res) => {
          proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))

          open.value = false
          getList()
        })
      } else {
        addCksTechnicalFlowNodes(form.value).then((res) => {
          proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))
          open.value = false
          getList()
        })
      }
    }
  })
}

// 允许重复工作按钮操作
function handleAllowReprocess(value, flag) {
  // console.log('handleAllowReprocess', value,flag);
  let lineName = '';

  if (flag == 1) {
    lineName = 'PCBA'
  } else if (flag == 2) {
    lineName = 'Radiator'
  } else if (flag == 3) {
    lineName = 'EOL'
  }

  proxy
    .$confirm(proxy.$t('process.ckstechnicalflownodesPage.page.allowReprocessConfirm'), proxy.$t("common.systemTips"), {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return allowReprocess(lineName, value)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))
    }).catch(() => {
      if (flag == 1) {
        pcbaValue.value = !value
      } else if (flag == 2) {
        coolingribValue.value = !value
      } else if (flag == 3) {
        eolValue.value = !value
      }
    });
}

handleQuery()
</script>